import Vue from "vue";
import Vuex from "vuex";
import {getUserInfo} from '@/api/getData';

Vue.use(Vuex)

const state = {
  userInfo: {
    avatar: 'default.jpg'
  },
}

const mutations = {
  saveUserInfo(state, userInfo) {
    state.userInfo = userInfo;
  }
}

const actions = {
  async getUserData({commit}) {
    try {
      const res = await getUserInfo();
      if (1 === res.status) {
        commit('saveUserInfo', res.data);
      } else {
        throw new Error(res);
      }
    } catch (e) {
      console.log('您尚未登陆或者session失效')
    }
  }
}

export default new Vuex.Store({
  state,
  actions,
  mutations
})
